#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>




typedef struct {
    int* a;
    int front;
    int rear;
    int k;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k)
{
    MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    if (obj == NULL)
    {
        perror("malloc");
        return NULL;
    }
    obj->a = (int*)malloc(sizeof(int) * (k + 1));
    if (obj->a == NULL)
    {
        perror("malloc");
        return NULL;
    }
    obj->front = obj->rear = 0;
    obj->k = k;
    return obj;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj)
{
    assert(obj);
    return obj->front == obj->rear;
}

bool myCircularQueueIsFull(MyCircularQueue* obj)
{
    assert(obj);
    return (obj->rear + 1) % (obj->k + 1) == obj->front;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value)
{
    assert(obj);
    if (myCircularQueueIsFull(obj))
        return false;
    else
    {
        obj->a[obj->rear] = value;
        obj->rear++;
        //obj->rear%=obj->k;
        if (obj->rear >= (obj->k + 1))
            obj->rear = obj->rear % (obj->k + 1);
    }
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj)
{
    assert(obj);
    if (myCircularQueueIsEmpty(obj))
        return false;
    else
    {
        obj->front++;
        if (obj->front >= (obj->k + 1))
            obj->front = obj->front % (obj->k + 1);
    }
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj)
{
    assert(obj);
    if (myCircularQueueIsEmpty(obj))
        return -1;
    else
    {
        return obj->a[obj->front];
    }
}

int myCircularQueueRear(MyCircularQueue* obj)
{
    assert(obj);
    if (myCircularQueueIsEmpty(obj))
        return -1;
    else
    {
        return obj->a[(obj->rear - 1 + obj->k + 1) % (obj->k + 1)];
    }
}



void myCircularQueueFree(MyCircularQueue* obj)
{
    free(obj->a);
    free(obj);
}

int main()
{
    int k = 3;
    MyCircularQueue* obj = myCircularQueueCreate(k);
    bool ret1 = myCircularQueueEnQueue(obj, 1);
    bool ret2 = myCircularQueueEnQueue(obj, 2);
    bool ret3 = myCircularQueueEnQueue(obj, 3);
    bool ret4 = myCircularQueueEnQueue(obj, 4);
    int rear1 = myCircularQueueRear(obj);
    bool retf = myCircularQueueIsFull(obj);
    bool ret5=myCircularQueueDeQueue(obj);
    bool ret6 = myCircularQueueEnQueue(obj, 4);
    int rear2 = myCircularQueueRear(obj);

}